package com.gxa.order.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.common.base.Constant;
import com.gxa.common.base.RespResult;
import com.gxa.common.base.RespResultCode;
import com.gxa.order.dto.ServiceOrderDTO;
import com.gxa.order.mapper.ServiceOrderMapper;
import com.gxa.order.service.ServiceOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;

/**
 * @author 33186
 * @description 针对表【service_order(服务订单表)】的数据库操作Service实现
 * @createDate 2023-04-10 13:58:44
 */
@Service
@Slf4j
public class ServiceOrderServiceImpl implements ServiceOrderService {

    @Autowired
    ServiceOrderMapper serviceOrderMapper;

    @Autowired
    RedisTemplate redisTemplate;

    Page<ServiceOrderDTO> page;

    /**
     * 查找所有服务订单
     * @param currentPage 当前页
     * @param pageSize 每一页条数
     */
    @Override
    public RespResult<Page> findAllServiceOrder(Integer currentPage, Integer pageSize) {

        //获取到redisTemplate对象
        ValueOperations valueOperations = redisTemplate.opsForValue();

        //序列化redisTemplate模板
        redisTemplate.setHashKeySerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());

        //从Redis获取到page
        page = (Page<ServiceOrderDTO> )valueOperations.get("findAllServiceOrderPage");

        if (page != null) {
//            输出日志数据
            log.info("直接读取redis数据"+page);
            return RespResult.success(page);
        }else if(currentPage != null && pageSize != null){
            page = serviceOrderMapper.selectAllServiceOrder(new Page<>(currentPage,pageSize));
            valueOperations.set("findAllServiceOrderPage",page,Constant.Expiration_TIME, TimeUnit.MINUTES);
        }
        return RespResult.success(page);
    }

    /**
     * 通过条件获取服务订单列表
     * @param currentPage 当前页
     * @param pageSize 每一页大小
     * @param condition 条件
     */
    @Override
    public RespResult<Page> findServiceOrderByCondition(Integer currentPage, Integer pageSize, String condition) {
        //获取到redisTemplate对象
        ValueOperations valueOperations = redisTemplate.opsForValue();

        //序列化redisTemplate模板
        redisTemplate.setHashKeySerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());

        //从Redis获取到page
        page = (Page<ServiceOrderDTO> )valueOperations.get("findServiceOrderByCondition");

        if (page != null) {
//            输出日志数据
            log.info("直接读取redis数据"+page);
            return RespResult.success(page);
        }else if(currentPage != null && pageSize != null){
            page = serviceOrderMapper.selectAllServiceOrderByCondition(new Page<>(currentPage,pageSize),condition);
            valueOperations.set("findServiceOrderByCondition",page, Constant.Expiration_TIME, TimeUnit.MINUTES);
        }
        return RespResult.success(page);
    }

    /**
     * 更换顾问
     * @param consultant 顾问
     * @param id 服务订单id
     */
    @Override
    public RespResult replaceConsultant(String consultant,Integer id) {
        serviceOrderMapper.replaceConsultant(consultant,id);
        return RespResult.success(RespResultCode.OK);
    }


}




